<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <!-- 头部左侧内容 -->
      <el-tabs class="tab" v-model="activeName">
        <el-tab-pane label="销售量" name="sale"></el-tab-pane>
        <el-tab-pane label="访问量" name="visit"></el-tab-pane>
      </el-tabs>
      <!-- 头部右侧内容 -->
      <div class="right">
        <span @click="setDay">今日</span>
        <span @click="setWeek">本周</span>
        <span @click="setMonth">本月</span>
        <span @click="setYear">本年</span>
        <el-date-picker
          type="datetimerange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['12:00:00']"
          size="mini"
          class="date"
          v-model="date"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </div>
    </div>
    <!-- 柱状图显示 -->
    <div>
      <el-row :gutter="10">
        <el-col :span="18">
          <!-- echarts容器 -->
          <div class="box1" ref="box"></div>
        </el-col>
        <el-col :span="6" class="col">
          <div>
            <h3>门店{{ title }}排名</h3>
          </div>
          <ul>
            <li>
              <span class="one">1</span>
              <span class="two">肯德基</span>
              <span class="three">213.241</span>
            </li>
            <li>
              <span class="one">2</span>
              <span class="two">麦当劳</span>
              <span class="three">213.241</span>
            </li>
            <li>
              <span class="one">3</span>
              <span class="two">德克士</span>
              <span class="three">213.241</span>
            </li>
            <li>
              <span class="one1">4</span>
              <span class="two">卡拉ok</span>
              <span class="three">213.241</span>
            </li>
            <li>
              <span class="one1">5</span>
              <span class="two">汉堡王</span>
              <span class="three">213.241</span>
            </li>
            <li>
              <span class="one1">6</span>
              <span class="two">海底捞</span>
              <span class="three">213.241</span>
            </li>
            <li>
              <span class="one1">7</span>
              <span class="two">烤肉</span>
              <span class="three">213.241</span>
            </li>
          </ul>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script>
// 引入echarts
import echarts from "echarts";
import dayjs from "dayjs";
export default {
  data() {
    return {
      activeName: "sale",
      mycharts: null,
      date: [], //收集日期
    };
  },
  // 初始化echarts实例
  mounted() {
    this.mycharts = echarts.init(this.$refs.box);
    this.mycharts.setOption({
      title: {
        text: "销售量趋势",
      },
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          data: [
            "一月",
            "二月",
            "三月",
            "四月",
            "五月",
            "六月",
            "七月",
            "八月",
            "九月",
            "十月",
            "十一月",
            "十二月",
          ],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          name: "Direct",
          type: "bar",
          barWidth: "60%",
          data: [10, 52, 200, 334, 390, 330, 220, 250, 300, 176, 80, 190],
          color: "yellowgreen",
        },
      ],
    });
  },
  //计算标题
  computed: {
    title() {
      return this.activeName == "sale" ? "销售量" : "访问量";
    },
  },
  //监听title的变化
  watch: {
    title() {
      // 重新修改图标的配置数据 有新的数值，用新的，没有，用旧的数值
      this.mycharts.setOption({
        title: {
          text: this.title + "趋势",
        },
      });
    },
  },
  methods: {
    //本日的时间
    setDay() {
      const day = dayjs().format("YYYY-MM-DD");
      this.date = [day, day];
    },
    //本周
    setWeek() {
      const start = dayjs().day(1).format("YYYY-MM-DD");
      const end = dayjs().day(7).format("YYYY-MM-DD");
      this.date = [start, end];
    },
    // 本月
    setMonth() {
      const start = dayjs().startOf("month").format("YYYY-MM-DD");
      const end = dayjs().endOf("month").format("YYYY-MM-DD");
      this.date = [start, end];
    },
    // 本年
    setYear() {
      const start = dayjs().startOf("year").format("YYYY-MM-DD");
      const end = dayjs().endOf("year").format("YYYY-MM-DD");
      this.date = [start, end];
    },
  },
};
</script>

<style scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
  position: relative;
}
.tab {
  width: 100%;
}
.right {
  position: absolute;
  right: 0px;
}
.date {
  width: 250px;
  margin: 0 20px;
}
.right span {
  margin: 0 15px;
}
.box1 {
  width: 100%;
  height: 300px;
}
.col {
  padding: 0px;
}
ul {
  width: 100%;
  height: 300px;
  list-style: none;
  padding: 0px;
}
ul li {
  height: 8%;
  margin: 10px 0;
}
.one,
.one1 {
  float: left;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: black;
  text-align: center;
  color: white;
}
.one1 {
  background-color: white;
  color: black;
}
.two {
  margin-left: 20px;
}
.three {
  float: right;
}
</style>